home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 582 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.3 KB  |  111 lines

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Semantics of "new foo[0]"
  5. Date: 28 Feb 1996 18:07:08 GMT
  6. Organization: FakultΣt fⁿr Mathematik und Informatik
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4h22m5$pll@news.BelWue.DE>
  9. References: <MGB.96Feb27175316@kronecker.mitre.org>
  10. Reply-To: dietmar.kuehl@uni-konstanz.de
  11. NNTP-Posting-Host: taumet.eng.sun.com
  12. Content-Type: text
  13. X-Nntp-Posting-Host: uzwil.informatik.uni-konstanz.de
  14. X-Newsreader: TIN [version 1.2 PL2]
  15. Content-Length: 3424
  16. X-Lines: 84
  17. Originator: clamage@taumet
  18.  
  19. Hi,
  20. G. Mike Butler D054 (mgb@kronecker.mitre.org) wrote:
  21. : According to the ARM section 5.3.3:
  22. :      "operator new() can be called with the argument
  23. :       zero.  Repeated such calls return pointers to
  24. :       distinct objects."  
  25.  
  26. : But when I execute the following:
  27. :     #include <isotream.h>
  28. :     main()
  29. :     {
  30. :       struct foo { char a[1024]; };
  31. :       foo *p = new foo[0];
  32. :       foo *q = new foo[0];
  33.  
  34. According to the DWP, 'new foo[x]' calls 'operator new[](y)' to
  35. allocate the memory (see expr.new section 10 of the DWP) instead of
  36. 'operator new(y)'. This is a change over the ARM. Thus, I use 'operator
  37. new[]()' in the discussion below.
  38.  
  39. Note, that this MIGHT call 'operator new[](0)' but might also call
  40. 'operator new[]()' with some different argument: The implementation is
  41. allowed to store additional information, e.g. the number of objects
  42. stored in the memory. If you want the check the values returned by
  43. 'operator new[]()' you have to call it directly, e.g.:
  44.  
  45.     void *ptr = operator new[](0);
  46.  
  47. ... and release the memory allocated this way with 'operator delete[]()':
  48.  
  49.     operator delete[](ptr);
  50.  
  51. This mechanism is used to allocate "raw" (i.e. uninitialized) memory.
  52. If you call 'new foo[x]' the compiler will invoke 'operator new[](y)'
  53. according to the DWP. But it performs additional actions: It calls the
  54. default ctor of 'foo' 'x' times to initialize the 'x' elements and
  55. somehow remembers the 'x' to be used for later destruction when calling
  56. 'delete[] ...' (if it is necessary to remember the 'x').
  57.  
  58. :       cout << (void *)p << " " << (void *)q << endl;
  59. :     }
  60.  
  61. : I get
  62. :     0x338d8 0x338e8
  63.  
  64. : While these are distinct pointers, the pointers refer to overlapping
  65. : objects. Is this a legitimate interpretation of section 5.3.3 or is
  66. : this a compiler/library bug?
  67.  
  68. The problem is that you are mixing 'new foo[0]' and 'operator
  69. new[](0)'.  The objects referred to in the description of 'operator
  70. new()' in the ARM are of an arbitary type, e.g. 'char'.  Consider the
  71. following fragment:
  72.  
  73.   void *ptr1 = operator new[](0);
  74.   void *ptr2 = operator new[](0);
  75.  
  76. 'ptr1' and 'ptr2' can indicate two adjacent bytes e.g 'ptr1 == 0x338d8'
  77. and 'ptr2 == 0x338d9' (although I guess that it is unlikely that they
  78. will do this). The requirement you cited rules out that 'ptr1' ==
  79. 'ptr2'.  I'm not sure whether this still applies to the rule found in
  80. the DWP (It is not allowed to return a '0' pointer or to return a
  81. pointer to or within currently allocated storage but it seems not to be
  82. required to allocate any storage...).  The quote from the ARM does not
  83. mean that 'new foo[0]' allocates enough storage to hold a 'foo' (this
  84. was your interpretation of 'object').  It only means that two calls to
  85. 'new foo[0]' return two distinct pointers (without an intervening call
  86. 'delete'ing the object allocated with the first call to 'new foo[0]',
  87. of course).
  88.  
  89. BTW, it results in undefined behavior to dereference the pointer
  90. returned from a call to 'operator new(0)' or 'operator new[](0)'. The
  91. exact rules an be found in section basic.stc.dynamic.allocation of the
  92. DWP.
  93.  
  94. : Incidentally, the standard template library included with gcc 2.7.2
  95. : relies on "operator new(0)" working.
  96.  
  97. I don't see where your example contradicts what is found in the ARM
  98. or in the DWP.
  99. --
  100. dietmar.kuehl@uni-konstanz.de
  101. http://www.informatik.uni-konstanz.de/~kuehl
  102. I am a realistic optimist - that's why I appear to be slightly pessimistic
  103.  
  104.  
  105. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your
  106.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  107.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  108.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  109.   Comments? mailto:std-c++-request@ncar.ucar.edu
  110. ]
  111.